<template>
  <div>
    <!-- bidirectional data binding（双向数据绑定） -->
    <quill-editor v-model="addGoodsForm.goods_introduce"> </quill-editor>
    <el-button class="addBtn" type="primary" size="mini" @click="addGoods"
      >添加</el-button
    >
  </div>
</template>

<script>
import EventBus from '@/event-bus'
import { addGoodsAPI } from '@/api'
export default {
  name: 'GoodsContent',
  data() {
    return {}
  },
  inject: ['addGoodsForm'],
  methods: {
    // 增加商品
    async addGoods() {
      // 校验表单
      EventBus.$emit('valid-form')
      const obj = JSON.parse(JSON.stringify(this.addGoodsForm))
      obj.goods_cat = obj.goods_cat.join(',')
      // 将参数改造成数组
      EventBus.$emit('push-attr')
      obj.attrs = this.addGoodsForm.attrs
      // console.log(obj)
      try {
        const { data } = await addGoodsAPI(obj)
        if (data.meta.status !== 201) {
          return this.$message.error(data.meta.msg)
        }
        this.$message.success('添加商品成功')
        this.$router.push({
          name: 'goods'
        })
      } catch (error) {
        this.$message.error('添加商品失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
/deep/.ql-editor {
  height: 400px !important;
}
.addBtn {
  margin-top: 15px;
}
</style>
